package com.eastfair.builder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.dto.ReportSiteDTO;
import com.eastfair.builder.dto.ReportSitePageQuery;
import com.eastfair.builder.dto.ReportSiteSaveDTO;
import com.eastfair.builder.dto.ReportSiteUpdateDTO;
import com.eastfair.builder.entity.ReportSite;
import com.eastfair.builder.service.ReportSiteService;
import com.eastfair.builder.service.impl.ReportSiteServiceImpl;
import com.eastfair.builder.vo.ReportSiteVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报馆展位基本信息
 * </p>
 *
 * @author dq
 * @date 2022-08-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportSite")
@Api(value = "ReportSite", tags = "报馆展位基本信息")
@PreAuth(replace = "builder:reportSite:")
public class ReportSiteController extends SuperController<ReportSiteService, Long, ReportSite, ReportSitePageQuery, ReportSiteSaveDTO, ReportSiteUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportSiteServiceImpl reportSiteService;

    @Override
    public void handlerResult(IPage<ReportSite> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportSite> reportSiteList = list
                .stream()
                .map((map) -> ReportSite.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportSiteList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportSitePageQuery> params) {
        ReportSitePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 报馆ID查询展位基本信息.
     *
     * @param reportSpaceId the report space id
     * @return the by report space id
     */
    @GetMapping("/reportSpace/{reportSpaceId}")
    public R<ReportSiteVO> getByReportSpaceId(@PathVariable("reportSpaceId") Long reportSpaceId) {
        log.info("getByReportSpaceId - 报馆ID查询展位基本信息, reportSpaceId={}", reportSpaceId);
        return R.successDef(reportSiteService.getVOByReportSpaceId(reportSpaceId));
    }

    /**
     * 保存报馆展位基本信息.
     *
     * @param reportSiteDTO the report site dto
     * @return the r
     */
    @SysLog("添加报馆展位基本信息")
    @PostMapping("/save")
    public R<ReportSite> saveReportSite(@RequestBody ReportSiteDTO reportSiteDTO) {
        log.info("saveReportSite - 保存报馆展位基本信息, reportSiteDTO={}", reportSiteDTO);
        return R.successDef(reportSiteService.saveReportSite(reportSiteDTO));
    }

    /**
     * 修改报馆展位基本信息.
     *
     * @param reportSiteDTO the report site dto
     * @return the r
     */
    @SysLog("修改报馆展位基本信息")
    @PostMapping("/update")
    public R<ReportSite> updateReportSite(@RequestBody ReportSiteDTO reportSiteDTO) {
        log.info("updateReportSite - 修改报馆展位基本信息, reportSiteDTO={}", reportSiteDTO);
        return R.successDef(reportSiteService.updateReportSite(reportSiteDTO));
    }
}
